package cn.ecnu.paper.mapper;

import cn.ecnu.paper.pojo.Field;
import cn.ecnu.paper.vo.FieldTree;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FieldMapper {

    @Select("SELECT * FROM field WHERE parent_id IS NULL")
    List<FieldTree> getRootField();

    @Select("SELECT * FROM field WHERE parent_id = #{parentId}")
    List<FieldTree> getFieldByParentId(@Param("parentId") Integer parentId);

    @Select("SELECT * FROM field WHERE parent_id IS NOT NULL")
    List<FieldTree> getFieldsList();

    int insertField(Field field);

    @Select("SELECT COUNT(*) FROM field WHERE parent_id = #{fieldId}")
    int countChild(@Param("fieldId") Integer fieldId);


    int deleteById(@Param("fieldId") Integer fieldId);

    @Select("SELECT * FROM field WHERE field_id = #{fieldId}")
    Field getFieldById(@Param("fieldId") Integer fieldId);

    @Update("UPDATE field SET parent_id = #{parentId}, field_name = #{fieldName} WHERE field_id = #{fieldId}")
    int updateField(Field field);
}
